package com.ai.module.user.controller;

import com.ai.module.chat.common.ano.NoAuth;
import com.ai.module.chat.common.ano.NoLog;
import com.ai.module.user.domain.entity.PublicPromptEntity;
import com.ai.module.user.domain.entity.PublicTitleEntity;
import com.ai.module.user.domain.enu.AIProductModelType;
import com.ai.module.user.domain.req.AiProductReq;
import com.ai.module.user.domain.req.ProductRecordReq;
import com.ai.module.user.domain.vo.*;
import com.ai.module.user.enu.AIType;
import com.ai.module.user.mapper.AiProductMapper;
import com.ai.module.user.mapper.PublicPromptMapper;
import com.ai.module.user.service.PublicTitleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.ai.module.user.controller.TestUtil.getImageTemplateMap;

/**
 * @author: heJinQin
 * @create: 2024-03-25 18:27
 * @description:
 **/
@RestController
@RequestMapping(("member/title"))
public class PublicTitleController {

    @Autowired
    private PublicTitleService titleService;

    @Autowired
    private PublicPromptMapper publicPromptMapper;

    @Autowired
    private AiProductMapper aiProductMapper;

    @GetMapping
    @NoLog
    public List<PublicTitleVo> imagePrompt(){
        return titleService.imagePrompt();
    }

    @GetMapping("/test")
    @NoAuth
    public void test(){
        Map<String, List<String>> map = getImageTemplateMap();
        Set<String> keySet = map.keySet();
        List<PublicPromptEntity> publicPromptList = new ArrayList<>();
        for (String title : keySet) {
            PublicTitleEntity publicTitle = new PublicTitleEntity();
            publicTitle.setTitle(title);
            titleService.save(publicTitle);
            var uuid = publicTitle.getUuid();
            List<String> promptList = map.get(title);
            for (String prompt : promptList) {
                PublicPromptEntity publicPrompt = new PublicPromptEntity();
                publicPrompt.setPrompt(prompt);
                publicPrompt.setTitleId(uuid);
                publicPromptMapper.insert(publicPrompt);
            }
        }
    }



   @GetMapping("/testAiProduct")
    @NoAuth
    public void testAiProduct(){
//        dealAiProduct(titleService,aiProductMapper);

    }

    @GetMapping("/aiProduct")
    @NoAuth
    @NoLog
    public List<AIProductVo> aiProduct(AIType aiType){
        if ( aiType == null){
             aiType = AIType.aiProduct;
        }
        return titleService.aiProduct(aiType);
    }

    @GetMapping("/aiImageProduct")
    @NoAuth
    @NoLog
    public AiProductContent aiImageProduct(AIProductModelType aiType){
        if ( aiType == null){
            aiType = AIProductModelType.MID_IMAGE;
        }
        return titleService.aiImageProduct(aiType);
    }

    @PostMapping("/productChat")
    public UserProductVo productChat(@RequestBody AiProductReq aiProductReq){
        String message = aiProductReq.getMessage();
        if (StringUtils.hasText(message)){
            String realPrompt = message.replaceAll("—", "-")
                    .replaceAll("：", ":");
            aiProductReq.setMessage(realPrompt);
        }
        aiProductReq.setStream(false);
        return titleService.productChat(aiProductReq);

    }

    @PostMapping("/productRecord")
    public IPage<AiProductContentRecordVo> productRecord(@RequestBody ProductRecordReq productRecordReq){
        return titleService.productRecord(productRecordReq);
    }

    @GetMapping("/findAiProduct")
    @NoAuth
    public AiProductContent findAiProduct( String aiProductId){

        return titleService.findAiProduct(aiProductId);
    }

    @GetMapping("imageMange")
    @NoLog
    @NoAuth
    public List<ImageMangeVo> imageMange(){
        return titleService.imageMange();
    }


}
